<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.3/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","images":"/images","scheme":"Muse","version":"8.3.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"}};
  </script>
<meta name="description" content="python 数据结构Python有四种内置的数据结构  列表(List) 元组(Tuple) 字典(Dictionary) 集合(Set)  列表shoplist &#x3D; [&amp;#x27;apple&amp;#x27;,&amp;#x27;mango&amp;#x27;,&amp;#x27;carrot&amp;#x27;,&amp;#x27;banana&amp;#x27;]print(&amp;#x27;I have&amp;#x27;,len(shoplist),&amp;">
<meta property="og:type" content="article">
<meta property="og:title" content="Python - 数据结构">
<meta property="og:url" content="http://example.com/2021/04/25/Python/python-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/index.html">
<meta property="og:site_name" content="Lawted&#39;s Blog">
<meta property="og:description" content="python 数据结构Python有四种内置的数据结构  列表(List) 元组(Tuple) 字典(Dictionary) 集合(Set)  列表shoplist &#x3D; [&amp;#x27;apple&amp;#x27;,&amp;#x27;mango&amp;#x27;,&amp;#x27;carrot&amp;#x27;,&amp;#x27;banana&amp;#x27;]print(&amp;#x27;I have&amp;#x27;,len(shoplist),&amp;">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2021-04-25T13:13:22.000Z">
<meta property="article:modified_time" content="2021-05-07T01:34:08.984Z">
<meta property="article:author" content="Lawted Wu">
<meta property="article:tag" content="python">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://example.com/2021/04/25/Python/python-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'en'
  };
</script>
<title>Python - 数据结构 | Lawted's Blog</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">Lawted's Blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#python-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84"><span class="nav-number">1.</span> <span class="nav-text">python 数据结构</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%88%97%E8%A1%A8"><span class="nav-number">1.1.</span> <span class="nav-text">列表</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%85%83%E7%BB%84"><span class="nav-number">1.2.</span> <span class="nav-text">元组</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AD%97%E5%85%B8"><span class="nav-number">1.3.</span> <span class="nav-text">字典</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%BA%8F%E5%88%97"><span class="nav-number">1.4.</span> <span class="nav-text">序列</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%9B%86%E5%90%88"><span class="nav-number">1.5.</span> <span class="nav-text">集合</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%BC%95%E7%94%A8"><span class="nav-number">1.6.</span> <span class="nav-text">引用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2plus"><span class="nav-number">1.7.</span> <span class="nav-text">字符串plus</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">Lawted Wu</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">67</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/LAWTED" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;LAWTED" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>



        </div>
      </div>
        <div class="back-to-top animated" role="button" aria-label="Back to top">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="en">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/04/25/Python/python-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Lawted Wu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lawted's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Python - 数据结构
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-04-25 21:13:22" itemprop="dateCreated datePublished" datetime="2021-04-25T21:13:22+08:00">2021-04-25</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Edited on</span>
        <time title="Modified: 2021-05-07 09:34:08" itemprop="dateModified" datetime="2021-05-07T09:34:08+08:00">2021-05-07</time>
      </span>

  
    <span class="post-meta-item" title="Views" id="busuanzi_container_page_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">Views: </span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="python-数据结构"><a href="#python-数据结构" class="headerlink" title="python 数据结构"></a>python 数据结构</h1><p>Python有四种内置的数据结构</p>
<ul>
<li>列表(List)</li>
<li>元组(Tuple)</li>
<li>字典(Dictionary)</li>
<li>集合(Set)</li>
</ul>
<h2 id="列表"><a href="#列表" class="headerlink" title="列表"></a>列表</h2><figure class="highlight python"><table><tr><td class="code"><pre><span class="line">shoplist = [<span class="string">&#x27;apple&#x27;</span>,<span class="string">&#x27;mango&#x27;</span>,<span class="string">&#x27;carrot&#x27;</span>,<span class="string">&#x27;banana&#x27;</span>]</span><br><span class="line"></span><br><span class="line">print(<span class="string">&#x27;I have&#x27;</span>,<span class="built_in">len</span>(shoplist),<span class="string">&#x27;items to purchase&#x27;</span>)</span><br><span class="line"></span><br><span class="line">print(<span class="string">&#x27;These items are: &#x27;</span>,end= <span class="string">&#x27;&#x27;</span>)</span><br><span class="line"><span class="keyword">for</span> item <span class="keyword">in</span> shoplist:</span><br><span class="line">    print(item,end = <span class="string">&#x27;&#x27;</span>)</span><br><span class="line"></span><br><span class="line">print(<span class="string">&#x27;\nI also have to buy rice.&#x27;</span>)</span><br><span class="line">shoplist.append(<span class="string">&#x27;rice&#x27;</span>)</span><br><span class="line">print(<span class="string">&#x27;My shopping list is now&#x27;</span>,shoplist)</span><br><span class="line"></span><br><span class="line">print(<span class="string">&#x27;I will sort my list now&#x27;</span>)</span><br><span class="line">shoplist.sort()</span><br><span class="line">print(<span class="string">&#x27;Sorted shopping list is&#x27;</span>,shoplist)</span><br><span class="line"></span><br><span class="line">print(<span class="string">&#x27;The first item I will buy is&#x27;</span>,shoplist[<span class="number">0</span>])</span><br><span class="line">olditem = shoplist[<span class="number">0</span>]</span><br><span class="line"><span class="keyword">del</span> shoplist[<span class="number">0</span>]</span><br><span class="line">print(<span class="string">&#x27;I bought the &#x27;</span>,olditem)</span><br><span class="line">print(<span class="string">&#x27;My shopping list is now&#x27;</span>,shoplist)</span><br><span class="line"></span><br><span class="line"><span class="comment">#I have 4 items to purchase</span></span><br><span class="line"><span class="comment">#These items are: applemangocarrotbanana</span></span><br><span class="line"><span class="comment">#I also have to buy rice.</span></span><br><span class="line"><span class="comment">#My shopping list is now [&#x27;apple&#x27;, &#x27;mango&#x27;, &#x27;carrot&#x27;, &#x27;banana&#x27;, &#x27;rice&#x27;]</span></span><br><span class="line"><span class="comment">#I will sort my list now</span></span><br><span class="line"><span class="comment">#Sorted shopping list is [&#x27;apple&#x27;, &#x27;banana&#x27;, &#x27;carrot&#x27;, &#x27;mango&#x27;, &#x27;rice&#x27;]</span></span><br><span class="line"><span class="comment">#The first item I will buy is apple</span></span><br><span class="line"><span class="comment">#I bought the  apple</span></span><br><span class="line"><span class="comment">#My shopping list is now [&#x27;banana&#x27;, &#x27;carrot&#x27;, &#x27;mango&#x27;, &#x27;rice&#x27;]</span></span><br></pre></td></tr></table></figure>
<p>列表是可变的<code>(Mutable)</code></p>
<h2 id="元组"><a href="#元组" class="headerlink" title="元组"></a>元组</h2><p>元组用于将多个对象保存在一起,但是元组是<code>(不可变的)</code>，不能编辑或者更改元组</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">zoo = (<span class="string">&#x27;python&#x27;</span>,<span class="string">&#x27;elephant&#x27;</span>,<span class="string">&#x27;penguin&#x27;</span>)</span><br><span class="line">print(<span class="string">&#x27;Number of animals in the zoo is &#x27;</span>,<span class="built_in">len</span>(zoo))</span><br><span class="line"></span><br><span class="line">new_zoo = <span class="string">&#x27;mokey&#x27;</span>,<span class="string">&#x27;camel&#x27;</span>,zoo</span><br><span class="line">print(<span class="string">&#x27;Number of cages in the new zoo is &#x27;</span>,<span class="built_in">len</span>(new_zoo))</span><br><span class="line">print(<span class="string">&#x27;All animals in new zoo are&#x27;</span>,new_zoo)</span><br><span class="line">print(<span class="string">&#x27;Animals brought from old zoo are &#x27;</span>,new_zoo[<span class="number">2</span>])</span><br><span class="line">print(<span class="string">&#x27;Last animal brought from old zoo is &#x27;</span>,new_zoo[<span class="number">2</span>][<span class="number">2</span>])</span><br><span class="line">print(<span class="string">&#x27;Number of animals in the new zoo is &#x27;</span>,<span class="built_in">len</span>(new_zoo)-<span class="number">1</span>+<span class="built_in">len</span>(new_zoo[<span class="number">2</span>]))</span><br><span class="line"></span><br><span class="line"><span class="comment">#Number of animals in the zoo is  3</span></span><br><span class="line"><span class="comment">#Number of cages in the new zoo is  3</span></span><br><span class="line"><span class="comment">#All animals in new zoo are (&#x27;mokey&#x27;, &#x27;camel&#x27;, (&#x27;python&#x27;, &#x27;elephant&#x27;, &#x27;penguin&#x27;))</span></span><br><span class="line"><span class="comment">#Animals brought from old zoo are  (&#x27;python&#x27;, &#x27;elephant&#x27;, &#x27;penguin&#x27;)</span></span><br><span class="line"><span class="comment">#Last animal brought from old zoo is  penguin</span></span><br><span class="line"><span class="comment">#Number of animals in the new zoo is  5</span></span><br></pre></td></tr></table></figure>


<h2 id="字典"><a href="#字典" class="headerlink" title="字典"></a>字典</h2><p><code>&#123;key,value&#125;</code>类似<code>map</code>的结构</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">ab = &#123;</span><br><span class="line">    <span class="string">&#x27;BUPT&#x27;</span>:<span class="string">&#x27;Changping&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;PKU&#x27;</span>:<span class="string">&#x27;Haidian&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;THU&#x27;</span>:<span class="string">&#x27;Haidian&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;WHU&#x27;</span>:<span class="string">&#x27;Wuhan&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line">print(<span class="string">&quot;BUPT&#x27;s address is &quot;</span>,ab[<span class="string">&#x27;BUPT&#x27;</span>])</span><br><span class="line"><span class="keyword">del</span> ab[<span class="string">&#x27;PKU&#x27;</span>]</span><br><span class="line"></span><br><span class="line">print(<span class="string">&#x27;\nTHere are &#x27;</span>,<span class="built_in">len</span>(ab),<span class="string">&#x27; contacts in the address-book\n&#x27;</span>)</span><br><span class="line"><span class="keyword">for</span> name,address <span class="keyword">in</span> ab.items():</span><br><span class="line">    print(<span class="string">&#x27;Contact &#x27;</span>,name,<span class="string">&#x27;at &#x27;</span>,address)</span><br><span class="line"></span><br><span class="line">ab[<span class="string">&#x27;HUST&#x27;</span>] = <span class="string">&#x27;Wuhan&#x27;</span></span><br><span class="line"><span class="keyword">if</span> <span class="string">&#x27;HUST&#x27;</span> <span class="keyword">in</span> ab:</span><br><span class="line">    print(<span class="string">&#x27;\nHUST is in &#x27;</span>,ab[<span class="string">&#x27;HUST&#x27;</span>])</span><br><span class="line"></span><br><span class="line"><span class="comment">#BUPT&#x27;s address is  Changping</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#THere are  3  contacts in the address-book</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#Contact  BUPT at  Changping</span></span><br><span class="line"><span class="comment">#Contact  THU at  Haidian</span></span><br><span class="line"><span class="comment">#Contact  WHU at  Wuhan</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#HUST is in  Wuhan</span></span><br></pre></td></tr></table></figure>


<h2 id="序列"><a href="#序列" class="headerlink" title="序列"></a>序列</h2><p>序列的主要功能是<code>资格测试</code>和<code>索引操作</code>，他们能够允许直接获取序列中的特定项目，上面提到的列表、元组和字符串都拥有一种<code>slicing</code>（切片）运算</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">shoplist = [<span class="string">&#x27;apple&#x27;</span>,<span class="string">&#x27;mango&#x27;</span>,<span class="string">&#x27;carrot&#x27;</span>,<span class="string">&#x27;banana&#x27;</span>]</span><br><span class="line">name = <span class="string">&#x27;Lawted&#x27;</span></span><br><span class="line">print(<span class="string">&#x27;Item 1 is &#x27;</span>,shoplist[<span class="number">0</span>])   <span class="comment">#Item 1 is  apple</span></span><br><span class="line">print(<span class="string">&#x27;Item 2 is &#x27;</span>,shoplist[<span class="number">1</span>])   <span class="comment">#Item 2 is  mango</span></span><br><span class="line">print(<span class="string">&#x27;Item 3 is &#x27;</span>,shoplist[<span class="number">2</span>])   <span class="comment">#Item 3 is  carrot</span></span><br><span class="line">print(<span class="string">&#x27;Item 4 is &#x27;</span>,shoplist[<span class="number">3</span>])   <span class="comment">#Item 4 is  banana</span></span><br><span class="line">print(<span class="string">&#x27;Item -1 is &#x27;</span>,shoplist[-<span class="number">1</span>]) <span class="comment">#Item -1 is  banana</span></span><br><span class="line">print(<span class="string">&#x27;Item -2 is &#x27;</span>,shoplist[-<span class="number">2</span>]) <span class="comment">#Item -2 is  carrot</span></span><br><span class="line">print(<span class="string">&#x27;Character 0 is&#x27;</span>,name[<span class="number">0</span>])   <span class="comment">#Character 0 is L</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#Slicing on a list</span></span><br><span class="line">print(<span class="string">&#x27;Item 1 to 3 is&#x27;</span>,shoplist[<span class="number">1</span>:<span class="number">3</span>])     <span class="comment">#Item 1 to 3 is [&#x27;mango&#x27;, &#x27;carrot&#x27;]</span></span><br><span class="line">print(<span class="string">&#x27;Item 2 to end is&#x27;</span>,shoplist[<span class="number">2</span>:])    <span class="comment">#Item 2 to end is [&#x27;carrot&#x27;, &#x27;banana&#x27;]</span></span><br><span class="line">print(<span class="string">&#x27;Item 1 to -1 is&#x27;</span>,shoplist[<span class="number">1</span>:-<span class="number">1</span>])   <span class="comment">#Item 1 to -1 is [&#x27;mango&#x27;, &#x27;carrot&#x27;]</span></span><br><span class="line">print(<span class="string">&#x27;Item start to end is&#x27;</span>,shoplist[:]) <span class="comment">#Item start to end is [&#x27;apple&#x27;, &#x27;mango&#x27;, &#x27;carrot&#x27;, &#x27;banana&#x27;]</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#Slicing on a string</span></span><br><span class="line">print(<span class="string">&#x27;Character 1 to 3&#x27;</span>,name[<span class="number">1</span>:<span class="number">3</span>])        <span class="comment">#Character 1 to 3 aw</span></span><br><span class="line">print(<span class="string">&#x27;Character 2 to end is&#x27;</span>,name[<span class="number">2</span>:])    <span class="comment">#Character 2 to end is wted</span></span><br><span class="line">print(<span class="string">&#x27;Character 1 to -1 is&#x27;</span>,name[<span class="number">1</span>:-<span class="number">1</span>])   <span class="comment">#Character 1 to -1 is awte</span></span><br><span class="line">print(<span class="string">&#x27;Character start to end is&#x27;</span>,name[:]) <span class="comment">#Character start to end is Lawted</span></span><br></pre></td></tr></table></figure>
<p>索引操作使用复数，在这种情况下，位置技术将从队列的末尾开始，因此<code>shoplist[-1]</code>指的是序列最后一个项目，序列的切片将包括起始位置，但不包括结束位置</p>
<p>因此，<code>shoplist[:-1]</code>返回一组序列切片，从第一个到最后一个，但是不包括最后一个</p>
<p>同样的类似<code>for</code>循环，我们可以在切片操作中提供第三个参数，这一参数被视为切片的步长</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">shoplist = [<span class="string">&#x27;apple&#x27;</span>,<span class="string">&#x27;mango&#x27;</span>,<span class="string">&#x27;carrot&#x27;</span>,<span class="string">&#x27;banana&#x27;</span>]</span><br><span class="line">print(shoplist[::<span class="number">1</span>]) <span class="comment">#[&#x27;apple&#x27;, &#x27;mango&#x27;, &#x27;carrot&#x27;, &#x27;banana&#x27;]</span></span><br><span class="line">print(shoplist[::<span class="number">2</span>]) <span class="comment">#[&#x27;apple&#x27;, &#x27;carrot&#x27;]</span></span><br><span class="line">print(shoplist[::-<span class="number">1</span>])<span class="comment">#[&#x27;banana&#x27;, &#x27;carrot&#x27;, &#x27;mango&#x27;, &#x27;apple&#x27;]</span></span><br></pre></td></tr></table></figure>
<h2 id="集合"><a href="#集合" class="headerlink" title="集合"></a>集合</h2><p>集合<code>(set)</code>是简单对象的无序集合<code>(Collection)</code>当集合中的项目存在与否比起次序或其出现次数更加重要时候，就会使用集合，通过集合，测试某些对象的资格或情况，检查它们是否其他集合的子集，找到两个集合的交集</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">st = <span class="built_in">set</span>([<span class="string">&#x27;brazil&#x27;</span>,<span class="string">&#x27;russia&#x27;</span>,<span class="string">&#x27;india&#x27;</span>])</span><br><span class="line">print(<span class="string">&#x27;india&#x27;</span> <span class="keyword">in</span> st) </span><br><span class="line">print(<span class="string">&#x27;usa&#x27;</span> <span class="keyword">in</span> st)</span><br><span class="line">st1 = st.copy()</span><br><span class="line">st1.add(<span class="string">&#x27;china&#x27;</span>)</span><br><span class="line">print(st1.issuperset(st))</span><br><span class="line">st1.remove(<span class="string">&#x27;russia&#x27;</span>)</span><br><span class="line">print(st &amp; st1)</span><br><span class="line"></span><br><span class="line"><span class="comment"># True</span></span><br><span class="line"><span class="comment"># False</span></span><br><span class="line"><span class="comment"># True</span></span><br><span class="line"><span class="comment"># &#123;&#x27;brazil&#x27;, &#x27;india&#x27;&#125;</span></span><br></pre></td></tr></table></figure>


<h2 id="引用"><a href="#引用" class="headerlink" title="引用"></a>引用</h2><p>当创建了一个对象并将其分配给某个变量时，变量会<code>refer</code>某个对象，并且它不会代表对象本身。他只是将名称<code>bingding </code>给了那一个对象</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">shoplist = [<span class="string">&#x27;apple&#x27;</span>,<span class="string">&#x27;mango&#x27;</span>,<span class="string">&#x27;carrot&#x27;</span>,<span class="string">&#x27;banana&#x27;</span>]</span><br><span class="line">mylist = shoplist</span><br><span class="line"><span class="keyword">del</span> shoplist[<span class="number">0</span>]</span><br><span class="line">print(<span class="string">&#x27;shoplist is &#x27;</span>,shoplist) <span class="comment"># shoplist is  [&#x27;mango&#x27;, &#x27;carrot&#x27;, &#x27;banana&#x27;]</span></span><br><span class="line">print(<span class="string">&#x27;mylist is &#x27;</span>,mylist)     <span class="comment"># mylist is  [&#x27;mango&#x27;, &#x27;carrot&#x27;, &#x27;banana&#x27;]</span></span><br><span class="line"></span><br><span class="line">mylist = shoplist[:]</span><br><span class="line"><span class="keyword">del</span> mylist[<span class="number">0</span>]</span><br><span class="line">print(<span class="string">&#x27;shoplist is &#x27;</span>,shoplist) <span class="comment"># shoplist is  [&#x27;mango&#x27;, &#x27;carrot&#x27;, &#x27;banana&#x27;]</span></span><br><span class="line">print(<span class="string">&#x27;mylist is &#x27;</span>,mylist)     <span class="comment"># mylist is  [&#x27;carrot&#x27;, &#x27;banana&#x27;]</span></span><br></pre></td></tr></table></figure>


<h2 id="字符串plus"><a href="#字符串plus" class="headerlink" title="字符串plus"></a>字符串plus</h2><figure class="highlight python"><table><tr><td class="code"><pre><span class="line">name = <span class="string">&#x27;Lawted&#x27;</span></span><br><span class="line"><span class="keyword">if</span> name.startswith(<span class="string">&#x27;Law&#x27;</span>):</span><br><span class="line">    print(<span class="string">&#x27;Yes it is start with Law&#x27;</span>)</span><br><span class="line"><span class="keyword">if</span> <span class="string">&#x27;a&#x27;</span> <span class="keyword">in</span> name:</span><br><span class="line">    print(<span class="string">&#x27;Yes it contains the string a&#x27;</span>)</span><br><span class="line"><span class="keyword">if</span> name.find(<span class="string">&#x27;ted&#x27;</span>) != -<span class="number">1</span>:</span><br><span class="line">    print(<span class="string">&#x27;Yes,it contains the string &quot;ted&quot;&#x27;</span>)</span><br><span class="line"></span><br><span class="line">delimiter = <span class="string">&#x27;-&#x27;</span></span><br><span class="line">mylist = [<span class="string">&#x27;Brazil&#x27;</span>,<span class="string">&#x27;USA&#x27;</span>,<span class="string">&#x27;China&#x27;</span>]</span><br><span class="line">print(delimiter.join(mylist))  <span class="comment">#Brazil-USA-China</span></span><br></pre></td></tr></table></figure>
<p>类似JS的写法</p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/python/" rel="tag"># python</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021/04/25/Python/python-%E6%A8%A1%E5%9D%97/" rel="prev" title="Python - 模块">
                  <i class="fa fa-chevron-left"></i> Python - 模块
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2021/05/07/%E9%9A%8F%E7%AC%94/2021%E5%B9%B4%E5%9B%9B%E6%9C%88/" rel="next" title="2021年四月">
                  2021年四月 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>







<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Lawted Wu</span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="Total Visitors">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="Total Views">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>

    </div>
  </footer>

  


  <script src="https://cdn.jsdelivr.net/npm/darkmode-js@1.5.7/lib/darkmode-js.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>


<script>
var options = {
  bottom: '64px', // default: '32px'
  right: '32px', // default: '32px'
  left: 'unset', // default: 'unset'
  time: '0.5s', // default: '0.3s'
  mixColor: '#fff', // default: '#fff'
  backgroundColor: '#fff',  // default: '#fff'
  buttonColorDark: '#000',  // default: '#100f2c'
  buttonColorLight: '#fff', // default: '#fff'
  saveInCookies: true, // default: true,
  label: ' ', // default: ''
  autoMatchOsTheme: true // default: true
}
const darkmode = new Darkmode(options);
darkmode.showWidget();
</script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  






  
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>





</body>
</html>
